package com.lm.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.jws.soap.SOAPBinding.Use;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;

import com.lm.dao.ArmyinfoMapper;
import com.lm.model.InfoAccStateNo;
import com.lm.model.InfoAccStateYes;
import com.lm.model.InfoBase;
import com.lm.model.InfoEconomy;
import com.lm.model.InfoFamily;
import com.lm.model.InfoPolicy;
import com.lm.model.User;
import com.lm.service.ArmyinfoService;
import com.mysql.fabric.xmlrpc.base.Data;
@Service
public class ArmyinfoServiceImpl implements ArmyinfoService {
	Date date = new Date();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	@Autowired
	private ArmyinfoMapper armyinfoMapper;

	/**
	 * 根据身份id查询基本信息
	 */
	public InfoBase findInfoBaseByIdcard(String idcard) {
		return armyinfoMapper.findInfoBaseByIdcard(idcard);
	}

	/**
	 * 根据身份id查询军人的家庭信息
	 * @param idcard
	 * @return
	 */
	public InfoFamily findInfoFamilyByIdcard(String idcard) {
		return armyinfoMapper.findInfoFamilyByIdcard(idcard);
	}

	/**
	 * 根据身份id查询军人的家庭经济情况
	 * @param idcard
	 * @return
	 */
	public InfoEconomy findInfoEconomyByIdcard(String idcard) {
		return armyinfoMapper.findInfoEconomyByIdcard(idcard);
	}

	/**
	 * 根据idcard查询其他退役军人基本情况
	 * @param idcard
	 * @return
	 */
	public InfoAccStateNo findInfoAccStateNoByIdcard(String idcard) {
		return armyinfoMapper.findInfoAccStateNoByIdcard(idcard);
	}

	/**
	 * 根据idcard查询符合政府安排工作条件的退役士兵基本情
	 * @param idcard
	 * @return
	 */
	public InfoAccStateYes findInfoAccStateYesByIdcard(String idcard) {
		return armyinfoMapper.findInfoAccStateYesByIdcard(idcard);
	}

	/**
	 * 根据idcard查询政策等信息
	 * @param idcard
	 * @return
	 */
	public InfoPolicy findInfoPolicyByIdcard(String idcard) {
		return armyinfoMapper.findInfoPolicyByIdcard(idcard);
	}

	/**
	 * 查询所有的军人基本信息集合
	 */
	public List<InfoBase> findAll() {
		return armyinfoMapper.findAll();
	}

	/**
	 * 根据身份idcard新增或修改基本信息
	 * @param infoBase
	 * @return
	 */
	public Integer saveInfoBase(HttpServletRequest request,InfoBase infoBase) {
		InfoBase info = new InfoBase();
		info = armyinfoMapper.findInfoBaseByIdcard(infoBase.getIdcard());
		if (info==null) {
			User user =(User) request.getSession().getAttribute("user");
			//不存在该idcard的信息，说明是新增
			infoBase.setCreate_dt(sdf.format(date));
			infoBase.setUpdate_dt(sdf.format(date));
			infoBase.setInput_user_id(user.getUser_id());
			infoBase.setInput_part_id(user.getPart_id());
			infoBase.setPower_id(user.getPower_id());
			return armyinfoMapper.insertInfoBase(infoBase);
		}else {
		//存在idcard的记录，说明是修改
			infoBase.setUpdate_dt(sdf.format(date));
			return armyinfoMapper.updateInfoBase(infoBase);
		}
	}

	/**
	 * 军人家庭信息修改
	 * @param infoFamily
	 * @return
	 */
	public Integer saveInfoFamily(HttpServletRequest request,InfoFamily infoFamily) {
		InfoFamily family =  new InfoFamily();
		family = armyinfoMapper.findInfoFamilyByIdcard(infoFamily.getIdcard());
		if (family==null) {//新增
			User user =(User) request.getSession().getAttribute("user");
			infoFamily.setCreate_dt(sdf.format(date));
			infoFamily.setUpdate_dt(sdf.format(date));
			infoFamily.setInput_user_id(user.getUser_id());
			infoFamily.setInput_part_id(user.getPart_id());
			infoFamily.setPower_id(user.getPower_id());
			return armyinfoMapper.insertInfoFamily(infoFamily);
		}else {//更新
			infoFamily.setUpdate_dt(sdf.format(date));
			return armyinfoMapper.updateInfoFamily(infoFamily);
		}
	}

	/**
	 * 修改军人的家庭经济情况
	 * @param infoEconomy
	 * @return
	 */
	public Integer saveInfoEconomy(HttpServletRequest request,InfoEconomy infoEconomy) {
		InfoEconomy info = new InfoEconomy();
		info = armyinfoMapper.findInfoEconomyByIdcard(infoEconomy.getIdcard());
		if(info==null) {
			//新增
			User user = (User) request.getSession().getAttribute("user");
			infoEconomy.setCreate_dt(sdf.format(date));
			infoEconomy.setUpdate_dt(sdf.format(date));
			infoEconomy.setInput_user_id(user.getUser_id());
			infoEconomy.setInput_part_id(user.getPart_id());
			infoEconomy.setPower_id(user.getPower_id());
			return armyinfoMapper.insertInfoEconomy(infoEconomy);
		}else {
			//更新
			infoEconomy.setUpdate_dt(sdf.format(date));
			armyinfoMapper.updateInfoInfoEconomy(infoEconomy);
		}
		return armyinfoMapper.updateInfoInfoEconomy(infoEconomy);
	}

	/**
	 * 修改符合政府安排工作条件的退役士兵基本情
	 * @param infoAccStateYes
	 * @return
	 */
	public Integer saveInfoAccStateYes(HttpServletRequest request,InfoAccStateYes infoAccStateYes) {
		InfoAccStateYes infoYes = new InfoAccStateYes();
		infoYes = armyinfoMapper.findInfoAccStateYesByIdcard(infoAccStateYes.getIdcard());
		if(infoYes==null) {
			//新增
			User user = (User) request.getSession().getAttribute("user");
			infoAccStateYes.setCreate_dt(sdf.format(date));
			infoAccStateYes.setUpdate_dt(sdf.format(date));
			infoAccStateYes.setInput_user_id(user.getUser_id());
			infoAccStateYes.setInput_part_id(user.getPart_id());
			infoAccStateYes.setPower_id(user.getPower_id());
			armyinfoMapper.insertInfoAccStateYes(infoAccStateYes);
		}else {
			//更新
			infoAccStateYes.setUpdate_dt(sdf.format(date));
			armyinfoMapper.updateInfoAccStateYes(infoAccStateYes);
		}
		return armyinfoMapper.updateInfoAccStateYes(infoAccStateYes);
	}

	/**
	 * 修改不符合政府安排工作条件的退役士兵基本情
	 * @param infoAccStateNo
	 * @return
	 */
	public Integer updateInfoAccStateNo(InfoAccStateNo infoAccStateNo) {
		return armyinfoMapper.updateInfoAccStateNo(infoAccStateNo);
	}

	/**
	 * 修改政策相关信息
	 * @param infoPolicy
	 * @return
	 */
	public Integer updateInfoPolicy(InfoPolicy infoPolicy) {
		return armyinfoMapper.updateInfoPolicy(infoPolicy);
	}

	/**
	 * 根据idcard删除基本信息
	 * @param idcard
	 * @return
	 */
	public Integer deleteInfoBase(String idcard) {
		return armyinfoMapper.deleteInfoBase(idcard);
	}

	/**
	 * 根据idcard删除家庭信息
	 * @param idcard
	 * @return
	 */
	public Integer deleteInfoFamily(String idcard) {
		return armyinfoMapper.deleteInfoFamily(idcard);
	}

	/**
	 * 根据idcard删除家庭经济情况信息
	 * @param idcard
	 * @return
	 */
	public Integer deleteInfoInfoEconomy(String idcard) {
		return armyinfoMapper.deleteInfoInfoEconomy(idcard);
	}

	/**
	 * 根据idcard删除符合政府安排工作条件的退役士兵基本情
	 * @author Administrator
	 *
	 */
	public Integer deleteInfoAccStateYes(String idcard) {
		return armyinfoMapper.deleteInfoAccStateYes(idcard);
	}

	/**
	 * 根据idcard删除不符合政府安排工作条件的退役士兵基本情
	 * @param idcard
	 * @return
	 */
	public Integer deleteInfoAccStateNo(String idcard) {
		return armyinfoMapper.deleteInfoAccStateNo(idcard);
	}

	/**
	 * 根据idcard删除政策信息
	 * @param idcard
	 * @return
	 */
	public Integer deleteInfoPolicy(String idcard) {
		return armyinfoMapper.deleteInfoPolicy(idcard);
	}
}
